package com.kkd.kkdapp.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.ScrollerLayout.OnPullListener;
import com.kkd.kkdapp.view.ScrollerLayout.ScrollStateProvider;
import com.kkd.kkdapp.view.xlistview.XListViewFooter;
import com.kkd.kkdapp.view.xlistview.XListViewHeader;

public class KkdXlistView extends RelativeLayout {

	private Context mContext;
	public ScrollerLayout mContentView;
	public KkdListHeader mHeaderView;
	private RelativeLayout mHeaderViewContent;
	// private TextView mHeaderTimeView;
	public ListView mListView;
	private XListViewFooter mFooterView;
	ScrollStateProvider mScrollStateProvider;
	private boolean canLoad = true;

	public void setBottomTip(String bottomTip) {
		mFooterView.setBottomTip(bottomTip);
	}

	private OnPullListener mScrollListener = new OnPullListener() {

		@Override
		public void onPullUp(float pull) {
		}

		@Override
		public void onPullStateChange(byte state) {
			mHeaderView.setY(0);
			switch (state) {
			case ScrollerLayout.STATUS_IDLE:// 空闲状态
				mHeaderView.setVisibility(View.GONE);
				mFooterView.setVisibility(View.GONE);
				break;
			case ScrollerLayout.STATUS_PULL_DOWN:// 下拉：不满足hold条件
				mHeaderView.setVisibility(View.VISIBLE);
				mHeaderView.setState(XListViewHeader.STATE_NORMAL);
				break;
			case ScrollerLayout.STATUS_PULL_UP:// 上拉：不满足hold条件
				mFooterView.setVisibility(View.VISIBLE);
				if (noMore) {
					mFooterView.setState(XListViewFooter.STATE_EMPTY);
				} else {
					mFooterView.setState(XListViewFooter.STATE_NORMAL);
				}

				break;
			case ScrollerLayout.STATUS_UP_HOLD:// 上拉后等待状态
				if (noMore == false) {
					if (mKKDScrollListener != null)
						mKKDScrollListener.onLoadMore();
					mFooterView.setState(XListViewFooter.STATE_LOADING);
				}
				break;
			case ScrollerLayout.STATUS_DOWN_HOLD:// 下拉后等待状态
				if (mKKDScrollListener != null)
					mKKDScrollListener.onRefresh();
				mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
				break;
			case ScrollerLayout.STATUS_ANIMATING:// 回弹动画状态
				break;
			case ScrollerLayout.STATUS_PULL_DOWN_ENABLE:// 下拉：满足hold条件，松开将hold
				mHeaderView.setState(XListViewHeader.STATE_READY);
				break;
			case ScrollerLayout.STATUS_PULL_UP_ENABLE: // 上拉：满足hold条件，松开将hold
				if (noMore == false) {
					mFooterView.setState(XListViewFooter.STATE_READY);
				}
				break;
			default:
				break;
			}

		}

		@Override
		public void onPullDown(float pull) {

		}
	};

	public KkdXlistView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initUI();
	}

	public KkdXlistView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mContext = context;
		initUI();
	}

	public KkdXlistView(Context context) {
		super(context);
		mContext = context;
		initUI();
	}

	private Handler mHandler = new Handler();
	private TextView tv_footer;
	private String footerTip = "没有更多内容了";
	private boolean isBottom = false;

	public void setFooterTextState(int flag) {
		if (mListView.getCount() > (mListView.getHeaderViewsCount() + mListView
				.getFooterViewsCount())) {
			tv_footer.setVisibility(View.VISIBLE);
		} else {
			tv_footer.setVisibility(View.GONE);
		}
		if (flag == 0) {
			tv_footer.setText("正在努力刷新中，请稍候...");
		} else if (flag == -1) {
			tv_footer.setText(footerTip);
		}
	}
	public void setFooterTip(String tip){
		footerTip = tip;
	}

	public void setFooterVisibile(int visi) {
		tv_footer.setVisibility(visi);
	}

	private void initUI() {
		mContentView = (ScrollerLayout) RelativeLayout.inflate(mContext,
				R.layout.kkd_xlist_layout, null);
		android.view.ViewGroup.LayoutParams params = new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		addView(mContentView, params);
		mHeaderView = new KkdListHeader(mContext);
		mHeaderView.setVisibility(View.GONE);
		mHeaderView.setY(SystemUtils.dip2px(mContext, 60));
		mHeaderViewContent = (RelativeLayout) mHeaderView
				.findViewById(R.id.xlistview_header_content);
		// mHeaderTimeView = (TextView) mHeaderView
		// .findViewById(R.id.xlistview_header_time);
		mContentView.addTopView(mHeaderView);
		mContentView.setOnPullListener(mScrollListener);
		mFooterView = new XListViewFooter(mContext);
		// mContentView.addBottomView(mFooterView);
		mListView = (ListView) mContentView.findViewById(R.id.list_view);

		mListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (isBottom && scrollState == SCROLL_STATE_IDLE && canLoad
						&& !noMore) {
					canLoad = false;
					mHandler.post(new Runnable() {

						@Override
						public void run() {
							if (mKKDScrollListener != null)
								mKKDScrollListener.onLoadMore();
						}
					});
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				isBottom = (firstVisibleItem + visibleItemCount) == totalItemCount;
			}
		});
		mFooterView.setBackgroundColor(Color.GREEN);
		tv_footer = new TextView(mContext);
		tv_footer.setText("正在努力刷新中,请稍候...");
		tv_footer.setTextColor(Color.parseColor("#c2c2c2"));
		tv_footer.setGravity(Gravity.CENTER);
		tv_footer.setPadding(0, SystemUtils.dip2px(mContext, 10), 0,
				SystemUtils.dip2px(mContext, 10));
		mListView.addFooterView(tv_footer);
		tv_footer.setVisibility(View.GONE);
		mScrollStateProvider = new ScrollStateProvider() {

			@Override
			public boolean canPullUp() {
				return false;
			}

			@Override
			public boolean canPullDown() {
				boolean isPull = false;
				if (mListView.getFirstVisiblePosition() == 0) {
					final View topChildView = mListView.getChildAt(0);
					if (topChildView != null)
						isPull = topChildView.getTop() >= 0;
					else
						isPull = true;
				}
				return isPull;
			}
		};
		mContentView.setScrollStateProvider(mScrollStateProvider);
	}

	KKDScrollListener mKKDScrollListener;

	public void setKKDScrollListener(KKDScrollListener callback) {
		mKKDScrollListener = callback;
	}

	public interface KKDScrollListener {
		public void onRefresh();

		public void onLoadMore();
	}

	public void initState() {
		mContentView.clearState();
	}

	public void stopRefresh() {
		canLoad = true;
		mHeaderView.setState(XListViewHeader.STATE_NORMAL);
		mHeaderView.setVisibility(View.GONE);
	}

	private boolean noMore = false;

	public void stopLoadMore(int flag) {
		if (flag == 0) {
			mContentView.clearState();
			setFooterTextState(0);
		} else if (flag == -1) {
			setNoMore(true);
			setFooterTextState(-1);
		} else if (flag == 1) {
			mContentView.clearState();
			setNoMore(true);
			setFooterTextState(-1);
		}

	}

	public void setNoMore(boolean more) {
		noMore = more;
	}

	public void setRefreshTime(String time) {
		// mHeaderTimeView.setText(time);
	}

	public void startRefresh() {
		mHeaderView.setVisibility(View.VISIBLE);
		mHeaderView.setState(XListViewHeader.STATE_REFRESHING);
		setRefreshTime(new Time_Date().get_Year_Day());
	}

	public void setScrollStateProvider(ScrollStateProvider mScrollStateProvider) {
		this.mScrollStateProvider = mScrollStateProvider;
		mContentView.setScrollStateProvider(mScrollStateProvider);
	}

	/**
	 * setAdapter:(给内嵌listview 添加adapter). <br/>
	 * 
	 * @author askzhao
	 * @param mAdapter
	 */
	public void setAdapter(BaseAdapter mAdapter) {
		mListView.setAdapter(mAdapter);

	}

	private float sp(final int sp) {
		float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
		return sp * scaledDensity;
	}
	
	public View getFooter(){
		return tv_footer;
	}
}
